<?php
/**
 * User: 发仔 <i@fazi.me>
 * Date: 2018/8/8
 */

namespace app\open\model;

use traits\model\SoftDelete;
use app\open\model\Role;
class User extends \app\adminapi\model\User
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $readonly = ['username'];

    public function area() {
        return $this->hasOne('Area','area_id','area_id','area','left')->setEagerlyType(0);
    }
    public function company() {
        return $this->hasOne('Company','company_id','company_id','company','left')->setEagerlyType(0);
    }

    //角色ID关联
    public function getRoleIdAttr($value,$data) {
        return Role::all($data['role_id']);
    }
    //分析权限
    public function getMenus() {
        $auth = [];
        $roles = $this->getAttr('RoleId');
        if($roles) {
            foreach ($roles AS $role) {
                $auth[] = $role->menus;
            }
        }
        return explode(',',implode(',', array_filter($auth)));
    }

    public function getOpenIDAttr($value,$data) {
        return $data['role_id'];
    }

    public function parseData($data)
    {
        if($data['password']) {
            $data['salt'] = substr(uniqid(), -6);
            $data['password'] = CorePwd($data['password'],$data['salt']);
        } else{
            unset($data['salt']);
            unset($data['password']);
        }
        return $data;
    }

}